﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HOPU.OnlineSchool.Application.Teachers.Dtos;
using HOPU.OnlineSchool.Core;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace HOPU.OnlineSchool.Application.Teachers.Services
{
    public class TeacherService : ITeacherService, ITransient
    {
        private readonly IRepository<EduTeacher> _teacherRepository;

        public TeacherService(IRepository<EduTeacher> teacherRepository)
        {
            this._teacherRepository = teacherRepository;
        }

        /// <summary>
        /// 查询教师
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task<PagedList<EduTeacher>> GetListSync(int page, int limit, TeacherSearchDto input)
        {
            var query = _teacherRepository.Where(m => m.IsDeleted == 0);
            if (!string.IsNullOrEmpty(input.Name))
            {
                query = query.Where(m => m.Name.Contains(input.Name));
            }
            if (input.Level > 0)
            {
                query = query.Where(m => m.Level == input.Level);
            }
            if (input.Begin.HasValue)
            {
                query = query.Where(m => m.GmtCreate >= input.Begin.Value);
            }
            if (input.End.HasValue)
            {
                query = query.Where(m => m.GmtCreate <= input.End.Value);
            }
            //分页
            var list = query.OrderByDescending(m => m.GmtCreate).ToPagedListAsync(page, limit);
            return list;
        }
        /// <summary>
        /// 删除讲师
        /// </summary>
        /// <param name="id"></param>
        public async Task<string> DeleteSync(string id)
        {
            var model = await _teacherRepository.FirstOrDefaultAsync(e => e.IsDeleted == 0 && e.Id == id);
            if (model == null)
                throw Oops.Oh("该讲师已经被删除");
            model.IsDeleted = 1;
            model.GmtModified = DateTime.Now;

            //更新指定的字段信息
            await _teacherRepository.UpdateIncludeAsync(model, new string[] { nameof(model.IsDeleted), nameof(model.GmtModified) });
            return "删除成功";
        }
        /// <summary>
        /// 添加讲师
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns> Task<string> AddSync(TeacherAddDto teacher);
        public async Task<string> AddSync(TeacherAddDto input)
        {
            var model = input.Adapt<EduTeacher>();//将添加数据模型转换为数据库数据模型

            //通过雪花算法生成主键id
            model.Id = YitIdHelper.NextId().ToString();
            await _teacherRepository.InsertAsync(model);
            return "添加成功";
        }
        /// <summary>
        /// 根据id获取讲师
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TeacherAddDto> GetTeacherAsync(string id)
        {
            var model = await _teacherRepository.FirstOrDefaultAsync(t => t.Id == id && t.IsDeleted == 0);
            if (model == null)
                throw Oops.Oh("该讲师未找到");
            return model.Adapt<TeacherAddDto>();
        }
        /// <summary>
        /// 更新讲师
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task UpdateAsync(string id, TeacherAddDto dto)
        {
            var exists = await _teacherRepository.AnyAsync(t => t.Id == id && t.IsDeleted == 0);
            if (!exists)
                throw Oops.Oh("该讲师未找到");
            var teacher = dto.Adapt<EduTeacher>();
            await teacher.UpdateExcludeAsync(new[] { nameof(teacher.GmtCreate), nameof(teacher.IsDeleted) });
        }
    }
}

